<template>
  <div class="followAdd" v-loading="detailInfoShow">
    <!-- 筛选按钮 -->
    <div
      class="search-button"
      v-if="pageType !== 'detail' && pageType !== 'check'"
    >
      <el-button
        v-if="pageType !== 'detail' && pageType !== 'check'"
        class="btn save"
        @click="handleAddSave(followFormRef)"
        ><el-icon class="icon" color="#fff"> <Tickets /> </el-icon
        >保存</el-button
      >
      <el-button class="btn back" @click="handleBack"
        ><el-icon class="icon" color="#fff"> <Back /> </el-icon
        >返回</el-button
      >
    </div>
    <el-collapse v-model="activeNames">
      <!-- 基础信息 -->
      <el-collapse-item
        name="1"
        v-if="pageType !== 'detail' && pageType !== 'check'"
        class="transform-down"
      >
        <template #title>
          <span class="circle"></span>
          <span class="collapse-title">添加跟进信息</span>
        </template>
        <div class="follow-info">
          <el-form
            :model="followForm"
            ref="followFormRef"
            :rules="queryRules"
            label-width="100px"
            v-enterToNext
            :disabled="pageType === 'detail'"
          >
            <el-row>
              <el-col :span="6">
                <el-form-item label="最后跟进：">
                  <el-date-picker
                    v-model="followForm.lastTime"
                    format="YYYY/MM/DD"
                    class="search-input"
                    value-format="YYYY-MM-DD"
                    type="date"
                    placeholder="无"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="下次跟进：" prop="followTime">
                  <el-date-picker
                    ref="refInput"
                    v-model="followForm.followTime"
                    format="YYYY/MM/DD"
                    class="search-input"
                    value-format="YYYY-MM-DD"
                    type="date"
                    placeholder="请选择"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="跟进内容：" prop="content">
                  <el-input
                    style="width: 100%"
                    v-model="followForm.content"
                    type="textarea"
                    maxlength="2000"
                    placeholder="请输入内容"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </el-collapse-item>
      <!-- 跟进记录 -->
      <el-collapse-item name="2" v-if="pageType == 'detail'">
        <template #title>
          <span class="circle"></span>
          <span class="collapse-title">跟进记录</span>
        </template>
        <div class="follow-detail-info">
          <!-- 表格数据 -->
          <el-table
            :data="followForm.tableData"
            border
            height="100%"
            :header-cell-style="{ background: 'red' }"
          >
            <el-table-column
              label="跟进时间"
              prop="createTime"
              :show-overflow-tooltip="true"
              width="150"
              align="center"
            />
            <el-table-column
              label="跟进内容"
              prop="content"
              :show-overflow-tooltip="true"
              min-width="160"
              align="center"
            />
            <el-table-column
              label="下次跟进时间"
              prop="followTime"
              :show-overflow-tooltip="true"
              width="150"
              align="center"
            />
          </el-table>
        </div>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script setup>
import {
  getEndFollowApi,
  addFollowApi,
  getCustomerDetailApi,
} from "@/api/information/customer";
import { ElMessage, ElMessageBox } from "element-plus";
const emit = defineEmits([
  // 刷新父组件表格数据
  "refreshList",
  // 更新跟进组件显隐
  "update:followAddShow",
]);
const props = defineProps({
  // 操作类型
  pageType: {
    type: String,
    default: "add",
  },
});
// 折叠栏默认展开
const activeNames = ref(["1", "2", "3"]);
// 客户表单引用
const followFormRef = ref();
// 详情加载效果
const detailInfoShow = ref(false);
// 输入框引用
const refInput = ref();
// 跟进表单引用
const followFormTemplate = {
  content: "",
  lastTime: "",
  followTime: "",
  customerId: null,
  tableData: [],
};
const data = reactive({
  // 跟进表单
  followForm: JSON.parse(JSON.stringify(followFormTemplate)),
  // 跟进表单校验规则
  queryRules: {
    content: [{ required: true, message: "请输入跟进内容", trigger: "blur" }],
    followTime: [
      { required: true, message: "请选择下次跟进", trigger: "change" },
    ],
  },
});
const { followForm, queryRules } = toRefs(data);

/**
 * @description: 保存
 * @param {Object} followFormRef 跟进表单引用
 */
const handleAddSave = async (followFormRef) => {
  if (!followFormRef) return;
  await followFormRef.validate(async (valid, fields) => {
    if (valid) {
      ElMessageBox.confirm("确定保存以上数据吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        confirmButtonClass: "confirmBtnClass",
        cancelButtonClass: "cancelBtnClass",
      })
        .then(async () => {
          const res = await addFollowApi(followForm.value);
          if (res.code === 200) {
            emit("update:followAddShow", false);
            emit("refreshList", false);
            ElMessage.success(res.msg);
            followForm.value = JSON.parse(JSON.stringify(followFormTemplate));
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {});
    }
  });
};

/**
 * @description: 返回
 */
const handleBack = () => {
  ElMessageBox.confirm("确定返回上一级吗?以上数据未保存将会丢失!", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    confirmButtonClass: "confirmBtnClass",
    cancelButtonClass: "cancelBtnClass",
  })
    .then(async () => {
      emit("update:followAddShow", false);
      followForm.value = JSON.parse(JSON.stringify(followFormTemplate));
    })
    .catch(() => {});
};

/**
 * @description: 处理详情信息
 * @param {Number} id
 */
const handleDetailInfo = async (id) => {
  try {
    followForm.value.customerId = id;
    if (props.pageType === "follow") {
      const res = await getEndFollowApi(id);
      if (res) {
        followForm.value.lastTime = res.createTime;
      }
    } else {
      const res = await getCustomerDetailApi(id);
      followForm.value.tableData = res.cusFollowUpRecordInfoVos;
    }
  } catch (error) {}
};

onMounted(() => {
  if (props.pageType !== "detail" && props.pageType !== "check") {
    nextTick(() => {
      setTimeout(() => {
        refInput.value.focus();
      }, 800);
    });
  }
});

defineExpose({
  handleDetailInfo,
});
</script>
<style lang="scss" scoped>
.followAdd {
  padding: 20px;
  width: 100%;
  height: 100%;
  .follow-info {
    padding: 20px 10px 5px 10px;
    background-color: #f0f0f0;
    margin: 10px 0;
  }
  .follow-detail-info {
    margin-top: 20px;
  }
}
</style>
